/******************************************************************************
 * @File name   :      CodeMappingController.java
 *
 * @Author      :      chen shixiang
 *
 * @Date        :      2011-4-26
 *
 * @Copyright Notice: 
 * Copyright (c) 2014 DMS, Inc. All  Rights Reserved.
 * This software is published under the terms of the DMS Software
 * License version 1.0, a copy of which has been included with this
 * distribution in the LICENSE.txt file.
 * 
 * 
 * ----------------------------------------------------------------------------
 * Date                   Who         Version        Comments
 * 2011-4-26 ä¸‹å�ˆ01:50:05        chen shixiang     1.0            Initial Version
 *****************************************************************************/
package com.cap.bts.framework.codemapping.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.cap.bts.framework.codemapping.domain.CodeInfo;
import com.cap.bts.framework.codemapping.dto.CodeInfoDto;
import com.cap.bts.framework.codemapping.service.ICodeMapping;
import com.cap.bts.framework.common.exception.AppException;

/**
 *
 */
@Controller
public class CodeMappingController {
    
    @Autowired
    private ICodeMapping codeMapping;
    
    @Autowired
    private CodeInfo codeInfo;
    
    
    /**
     * ç³»ç»Ÿä¸»æ•°æ�®
     * @Date        :      2011-4-27
     * @return
     */
    @RequestMapping(value = "/cm", method = RequestMethod.GET)
    public ModelAndView getJspPage() {
	CodeInfoDto codeInfoDto = new CodeInfoDto();
	Map<String, Object> map = new HashMap<String, Object>();
	map.put("types", getCodeMappingType());
	map.put("codeInfoDto", codeInfoDto);
	return new ModelAndView("sys/code_mapping",map);
    }
    
    /**
     * æŸ¥è¯¢Code info
     * @Date        :      2011-4-27
     * @param code
     * @return
     */
    @RequestMapping(value = "/get_codemapping/{code}", method = RequestMethod.GET)
    public @ResponseBody CodeInfo getCodeInfo(@PathVariable("code") String code) {
	CodeInfo codeInfo = null;
	try {
	    codeInfo = codeMapping.getCodeInfoByCode(code);
	} catch (AppException e) {
	    e.printStackTrace();
	}
        return codeInfo;
    }
    
    /**
     * æŸ¥è¯¢Code info
     * @Date        :      2011-4-27
     * @param codeInfoDto
     * @return
     */
    @RequestMapping(value = "/search_codemapping", method = RequestMethod.GET)
    public ModelAndView getCodeInfo(CodeInfoDto codeInfoDto) {
	List<CodeInfo> codeInfos = new ArrayList<CodeInfo>();
	try {
	    codeInfos = codeMapping.getCodeInfo(codeInfoDto);
	} catch (AppException e) {
	    e.printStackTrace();
	}
	Map<String, Object> map = new HashMap<String, Object>();
	map.put("codeInfoDto", codeInfoDto);
        map.put("codeInfos", codeInfos);
        map.put("types", getCodeMappingType());
        return new ModelAndView("sys/code_mapping", map);
    }
    
    /**
     * æ·»åŠ Code info
     * @Date        :      2011-4-27
     * @param codeInfo
     * @return
     */
    @RequestMapping(value = "/add_codemapping", method = RequestMethod.POST)
    public ModelAndView addCodeInfo(CodeInfo codeInfo) {
	try {
	    codeMapping.addCodeInfo(codeInfo);
	} catch (AppException e) {
	    e.printStackTrace();
	}
	return new ModelAndView("redirect:/search_codemapping");
    }
    
    /**
     * ä¿®æ”¹Code info
     * @Date        :      2011-4-27
     * @param codeInfoDto
     * @return
     */
    @RequestMapping(value = "/update_codemapping", method = RequestMethod.POST)
    public ModelAndView updateCodeInfo(CodeInfoDto codeInfoDto) {
	try {
	    codeMapping.updateCodeInfo(codeInfoDto);
	} catch (AppException e) {
	    e.printStackTrace();
	}
	return new ModelAndView("redirect:/search_codemapping");
    }
    
    /**
     *åˆ é™¤Code info
     * @Date        :      2011-4-27
     * @param code
     * @return
     */
    @RequestMapping(value = "/remove_codemapping/{code}", method = RequestMethod.GET)
    public @ResponseBody String removeCodeInfo(@PathVariable("code") String code) {
	String ecode = "";
	if (code != null && !"".equals(code)) {
	    String [] codes = code.split(",");
	    for(String c:codes) {
		if (c != null && !"".equals(c)) {
		    try {
			//System.out.println("============" + c);
			codeMapping.removeCodeInfoByCode(c);
		    } catch (AppException e) {
			  ecode = ecode + c + ",";
			  e.printStackTrace();
		    }
		}
	    }
	}
	if (!"".equals(ecode))
	    return ecode;
	
	return "true";
    }

    /**
     * @Date        :      2011-4-26
     *
     * @return the codeMapping
     */
    public ICodeMapping getCodeMapping() {
        return codeMapping;
    }

    /**
     * @Date        :      2011-4-26
     *
     * @param codeMapping the codeMapping to set
     */
    public void setCodeMapping(ICodeMapping codeMapping) {
        this.codeMapping = codeMapping;
    }

    /**
     * @Date        :      2011-4-27
     *
     * @return the codeInfo
     */
    public CodeInfo getCodeInfo() {
        return codeInfo;
    }

    /**
     * @Date        :      2011-4-27
     *
     * @param codeInfo the codeInfo to set
     */
    public void setCodeInfo(CodeInfo codeInfo) {
        this.codeInfo = codeInfo;
    }
    
    /**
     * @Date        :      2011-4-27
     * @return
     */
    private Map<String, String> getCodeMappingType() {
	Map<String, String> items = new LinkedHashMap<String, String>();
	if (codeInfo.getType() != null) {
	    String [] s = codeInfo.getType().split(",");
	    for(String ss : s) {
		items.put(ss, ss);
	    }
	}
	return items;
    }

}
